﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Content;

namespace theguineasyndrome.levels
{
    public class Levels
    {
        private List<Type> levels;
        private GameMode gameMode;
        private ContentManager content;

        private List<Score> scores;

        private WaterWeapon waterWeapon;
        private FireWeapon fireWeapon;
        private PortalWeapon portalWeapon;
        private PaintWeapon paintWeapon;

        public Levels(GameMode gameMode, ContentManager content, WaterWeapon waterWeapon, FireWeapon fireWeapon, PortalWeapon portalWeapon, PaintWeapon paintWeapon)
        {
            this.gameMode = gameMode;
            this.content = content;

            this.waterWeapon = waterWeapon;
            this.fireWeapon = fireWeapon;
            this.portalWeapon = portalWeapon;
            this.paintWeapon = paintWeapon;

            levels = new List<Type>();
            levels.Add(typeof(Tutorial));
            levels.Add(typeof(MarioLevel));
            levels.Add(typeof(PyroLevel));
            levels.Add(typeof(PortalLevel));
            levels.Add(typeof(EndLevel));

            scores = new List<Score>();

            foreach (Type t in levels)
                scores.Add(new Score());
        }

        public Level load(int n)
        {
            Type[] constructorParams = new Type[] {
                typeof(GameMode),
                typeof(ContentManager),
                typeof(WaterWeapon),
                typeof(FireWeapon),
                typeof(PortalWeapon),
                typeof(PaintWeapon)
            };
            object[] paramValues = new object[] {
                gameMode,
                content,
                waterWeapon,
                fireWeapon,
                portalWeapon,
                paintWeapon
            };
            LevelLoader levelLoader = (LevelLoader)levels[n].GetConstructor(constructorParams).Invoke(paramValues); // invokes the right constructor
            Level level = levelLoader.load();

            waterWeapon.Level = level;
            fireWeapon.Level = level;
            portalWeapon.Level = level;
            paintWeapon.Level = level;

            return level;
        }

        public void finish(int n, Score score)
        {
            scores[n] = score;
        }

        public bool isLastLevel(int n)
        {
            return n == levels.Count - 1;
        }

        internal Score getFinalScore()
        {
            int scoreSum = 0;
            int numCollectedCarots = 0;
            int numCarots = 0;

            foreach (Score score in scores)
            {
                scoreSum += score.TotalScore;
                numCollectedCarots += score.NumCollectedCarots;
                numCarots += score.NumCarots;
            }

            Score finalScore = new Score();
            finalScore.TotalScore = scoreSum;
            finalScore.NumCollectedCarots = numCollectedCarots;
            finalScore.NumCarots = numCarots;
            return finalScore;
        }
    }
}

